package com.huatai.exchange.bootup.controller;

import com.faneui.bootup.controller.AbstractRestController;
import com.faneui.common.bean.BaseResp;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.BeanMapper;
import com.faneui.common.utils.ListUtils;
import com.huatai.exchange.dal.model.ExcExchangeInfo;
import com.huatai.exchange.facade.ExchangeInfoController;
import com.huatai.exchange.facade.bean.ExchangeInfoVo;
import com.huatai.exchange.facade.bean.QueryExchangeInfoVoListResp;
import com.huatai.exchange.service.ExcExchangeInfoService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文件数据交换中心
 */
@RestController
@RequestMapping(value = "exchange")
@Api("客户数据文件配置")
public class ExchangeInfoControllerImpl extends AbstractRestController implements ExchangeInfoController {

    @Autowired
    private ExcExchangeInfoService exchangeInfoService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public QueryExchangeInfoVoListResp queryExchangeInfoVoList(@ModelAttribute ExchangeInfoVo exchangeInfoVo) throws BusinessException {
        QueryExchangeInfoVoListResp resp = new QueryExchangeInfoVoListResp();
        try {
            ExcExchangeInfo param = BeanMapper.map(exchangeInfoVo, ExcExchangeInfo.class);
            List<ExcExchangeInfo> infos = exchangeInfoService.queryExcExchangeInfoList(param);
            if (ListUtils.isNotEmpty(infos)) {
                List<ExchangeInfoVo> vos = BeanMapper.mapList(infos, ExchangeInfoVo.class);
                resp.setExchangeInfoVos(vos);
            }
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp addExchangeInfoVo(@RequestBody ExchangeInfoVo exchangeInfoVo) throws Exception {
        BaseResp resp = new BaseResp();
        try {
            ExcExchangeInfo exchangeInfo = BeanMapper.map(exchangeInfoVo,ExcExchangeInfo.class) ;
            exchangeInfoService.addExcExchangeInfo(exchangeInfo);
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp modifyExchangeInfoVo(@RequestBody ExchangeInfoVo exchangeInfoVo) throws Exception {
        BaseResp resp = new BaseResp();
        try {
            ExcExchangeInfo exchangeInfo = BeanMapper.map(exchangeInfoVo,ExcExchangeInfo.class) ;
            exchangeInfoService.modifyExcExchangeInfo(exchangeInfo);
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

}
